package cn.stylefeng.guns.modular.api;

import cn.stylefeng.guns.config.properties.GunsProperties;
import cn.stylefeng.guns.core.common.node.JsonResult;
import cn.stylefeng.guns.modular.api.model.DownloadFileDTO;
import cn.stylefeng.guns.modular.api.model.ModelDTO;
import cn.stylefeng.guns.modular.api.model.ModularDTO;
import cn.stylefeng.guns.modular.api.model.PageVo;
import cn.stylefeng.guns.modular.hp.constant.MenuJumpTypeEnum;
import cn.stylefeng.guns.modular.hp.model.ArticleInfo;
import cn.stylefeng.guns.modular.hp.model.FileDownloadInfo;
import cn.stylefeng.guns.modular.hp.model.MenuInfo;
import cn.stylefeng.guns.modular.hp.model.PresidentMailInfo;
import cn.stylefeng.guns.modular.hp.service.IArticleInfoService;
import cn.stylefeng.guns.modular.hp.service.IFileDownloadInfoService;
import cn.stylefeng.guns.modular.hp.service.IMenuInfoService;
import cn.stylefeng.guns.modular.hp.service.IPresidentMailInfoService;
import com.alibaba.druid.util.StringUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@RestController
@RequestMapping("/expand")
@Api(tags = "1.0", description = "市中医院门户拓展接口", value = "拓展接口API")
public class ExpandApiController {

	private final static Logger log = LoggerFactory.getLogger(ExpandApiController.class);

	@Autowired
	private GunsProperties gunsProperties;

	/**
	 * 院长信箱服务
	 */
	@Autowired
	private IPresidentMailInfoService presidentMailInfoService;

	@Autowired
	private IFileDownloadInfoService fileDownloadInfoService;

	@Autowired
	private IMenuInfoService menuInfoService;

	@Autowired
	private IArticleInfoService articleInfoService;

	@GetMapping("/saveMail")
    @ResponseBody
	@ApiOperation(value = "保存院长信箱内容")
	@ApiImplicitParams(value = {
			@ApiImplicitParam(name = "title", value = "信件标题(必须)", dataType = "String", required = true),
			@ApiImplicitParam(name = "content", value = "信件内容(必须)", dataType = "String", required = true),
			@ApiImplicitParam(name = "senderName", value = "发送者名字", dataType = "String"),
			@ApiImplicitParam(name = "senderPhone", value = "发送者电话", dataType = "String"),
			@ApiImplicitParam(name = "senderEmail", value = "发送者邮箱", dataType = "String")
	})
	public JsonResult getPage(@RequestParam String title, @RequestParam String content, String senderName, String senderPhone, String senderEmail) {
		JsonResult jr = new JsonResult(1 , "请求失败。", null);

		try{
			PresidentMailInfo presidentMailInfo = new PresidentMailInfo();
			presidentMailInfo.setTitle(title);
			presidentMailInfo.setContent(content);
			presidentMailInfo.setSenderName(senderName);
			presidentMailInfo.setSenderPhone(senderPhone);
			presidentMailInfo.setSenderEmail(senderEmail);
			presidentMailInfo.setCreatedTime(new Date());
			presidentMailInfo.setReadStatus(0);
			presidentMailInfoService.insert(presidentMailInfo);
			jr = new JsonResult(0 , "保存成功！", presidentMailInfo);
		}catch (Exception e){
			e.printStackTrace();
		}
		return jr;
	}

	@GetMapping("/getDownLoadFileList")
	@ResponseBody
	@ApiOperation(value = "获取文件下载列表")
	@ApiImplicitParams(value = {
			@ApiImplicitParam(name = "length", value = "获取列表的长度", dataType = "Integer", paramType = "query"),
			@ApiImplicitParam(name = "pageNo", value = "当前页数，以1为起始值", dataType = "Integer", paramType = "query")
	})
	public JsonResult<DownloadFileDTO> getDownLoadFileList(@RequestParam Integer length, @RequestParam Integer pageNo) {
		JsonResult jr = new JsonResult(1 , "请求失败。", null);
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
		try{
			if(pageNo != null && pageNo >= 1 && length > 0){
				List<DownloadFileDTO> fileDTOList = new ArrayList<>();
				Integer startIndex = length * (pageNo - 1);
				List<FileDownloadInfo> fileList = this.fileDownloadInfoService.fileDownloadInfoList(startIndex, length);
				Integer fileCnt = this.fileDownloadInfoService.fileDownloadInfoCont();
				for(FileDownloadInfo info : fileList){
					fileDTOList.add(new DownloadFileDTO(info.getFilename(),info.getDownloadUrl(),sdf.format(info.getCreatedTime())));
				}
				PageVo page = new PageVo(pageNo, length, fileCnt, fileDTOList);

				jr.setResultCode(0);
				jr.setResultMsg("请求成功！");
				jr.setResultData(page);
			}
		}catch (Exception e){
			e.printStackTrace();
		}
		return jr;
	}

	@GetMapping("/getModularlList")
	@ResponseBody
	@ApiOperation(value = "获取模块列表")
	public JsonResult<ModularDTO> getModularlList() {
		JsonResult jr = new JsonResult(1 , "请求失败。", null);

		try{
				List<MenuInfo> menuInfos = this.menuInfoService.modularInfolist();
				List<ModularDTO> modularDTOList = new ArrayList<>();

				for(MenuInfo info : menuInfos){
					modularDTOList.add(new ModularDTO(info.getName(), info.getCode(), info.getPri()));
				}

				jr.setResultCode(0);
				jr.setResultMsg("请求成功！");
				jr.setResultData(modularDTOList);
		}catch (Exception e){
			e.printStackTrace();
		}
		return jr;
	}

	@GetMapping("/searchModularInfo")
	@ResponseBody
	@ApiOperation(value = "搜索模块中的信息")
	@ApiImplicitParams(value = {
			@ApiImplicitParam(name = "modularCore", value = "模块ID", dataType = "String", paramType = "query"),
			@ApiImplicitParam(name = "keyword", value = "搜索关键字", dataType = "String", paramType = "query"),
			@ApiImplicitParam(name = "length", value = "获取列表的长度", dataType = "Integer", paramType = "query"),
			@ApiImplicitParam(name = "pageNo", value = "当前页数，以1为起始值", dataType = "Integer", paramType = "query")
	})
	public JsonResult<ModelDTO> searchModularInfo(@RequestParam String modularCore, @RequestParam String keyword, @RequestParam Integer length, @RequestParam Integer pageNo) {
		JsonResult jr = new JsonResult(1 , "请求失败。", null);

		try{
			if(!StringUtils.isEmpty(modularCore) && !StringUtils.isEmpty(keyword) && length != null && length >0 && pageNo != null && pageNo >0 ){
				Integer startIndex = length * (pageNo - 1);
				List<ArticleInfo> articleInfos = this.articleInfoService.searchModularInfo(modularCore.toLowerCase(), keyword, startIndex, length);
				Integer articleInfosCnt = this.articleInfoService.searchModularInfoCont(modularCore.toLowerCase(), keyword);

				List<ModelDTO> resultInfoList = new ArrayList<>();

				for(ArticleInfo info : articleInfos){
					resultInfoList.add(new ModelDTO(info.getTitle(), info.getPageContentId(), info.getPri(), info.getCreatedTime(), MenuJumpTypeEnum.DETAILS));
				}
				PageVo page = new PageVo(pageNo, length, articleInfosCnt, resultInfoList);

				jr.setResultCode(0);
				jr.setResultMsg("请求成功！");
				jr.setResultData(page);
			}
		}catch (Exception e){
			e.printStackTrace();
		}
		return jr;
	}
}
